package com.train.course.controller;

import com.train.common.core.controller.BaseController;
import com.train.common.core.domain.AjaxResult;
import com.train.common.core.domain.model.LoginUser;
import com.train.common.utils.SecurityUtils;
import com.train.course.service.IVideoCollectService;
import com.train.course.service.IVideoLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 视频交互管理 控制器
 * 统一处理点赞和收藏功能
 */
@RestController
@RequestMapping("/api/v1/videos")
public class VideoInteractionController extends BaseController {

    @Autowired
    private IVideoLikeService videoLikeService;

    @Autowired
    private IVideoCollectService videoCollectService;

    /**
     * 查询用户是否点赞视频
     */
    @GetMapping("/{videoId}/liked")
    public AjaxResult isVideoLiked(@PathVariable("videoId") Long videoId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        boolean isLiked = videoLikeService.isLiked(userId, videoId);
        return AjaxResult.success("查询成功", isLiked);
    }

    /**
     * 查询用户是否收藏视频
     */
    @GetMapping("/{videoId}/collected")
    public AjaxResult isVideoCollected(@PathVariable("videoId") Long videoId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        boolean isCollected = videoCollectService.isCollected(userId, videoId);
        return AjaxResult.success("查询成功", isCollected);
    }

    /**
     * 查询用户是否收藏课程
     */
    @GetMapping("/course/{courseId}/collected")
    public AjaxResult isCourseCollected(@PathVariable("courseId") Long courseId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        boolean isCollected = videoCollectService.isCourseCollected(userId, courseId);
        return AjaxResult.success("查询成功", isCollected);
    }

    /**
     * 点赞视频 - 切换模式（已点赞则取消点赞）
     */
    @PostMapping("/{videoId}/like")
    public AjaxResult likeVideo(@PathVariable("videoId") Long videoId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        
        // 检查是否已经点赞
        boolean isLiked = videoLikeService.isLiked(userId, videoId);
        boolean result;
        
        if (isLiked) {
            // 已点赞，执行取消点赞
            result = videoLikeService.unlikeVideo(userId, videoId);
            return result ? success("取消点赞成功") : error("取消点赞失败");
        } else {
            // 未点赞，执行点赞
            result = videoLikeService.likeVideo(userId, videoId);
            return result ? success("点赞成功") : error("点赞失败");
        }
    }

    /**
     * 取消点赞
     */
    @DeleteMapping("/{videoId}/like")
    public AjaxResult unlikeVideo(@PathVariable("videoId") Long videoId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        boolean result = videoLikeService.unlikeVideo(userId, videoId);
        return result ? success("取消点赞成功") : error("取消点赞失败");
    }

    /**
     * 收藏视频 - 切换模式（已收藏则取消收藏）
     */
    @PostMapping("/{videoId}/collect")
    public AjaxResult collectVideo(@PathVariable("videoId") Long videoId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        
        // 检查是否已经收藏
        boolean isCollected = videoCollectService.isCollected(userId, videoId);
        boolean result;
        
        if (isCollected) {
            // 已收藏，执行取消收藏
            result = videoCollectService.uncollectVideo(userId, videoId);
            return result ? success("取消收藏成功") : error("取消收藏失败");
        } else {
            // 未收藏，执行收藏
            result = videoCollectService.collectVideo(userId, videoId);
            return result ? success("收藏成功") : error("收藏失败");
        }
    }

    /**
     * 取消收藏
     */
    @DeleteMapping("/{videoId}/collect")
    public AjaxResult uncollectVideo(@PathVariable("videoId") Long videoId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        boolean result = videoCollectService.uncollectVideo(userId, videoId);
        return result ? success("取消收藏成功") : error("取消收藏失败");
    }
    
    /**
     * 收藏课程 - 切换模式（已收藏则取消收藏）
     */
    @PostMapping("/course/{courseId}/collect")
    public AjaxResult collectCourse(@PathVariable("courseId") Long courseId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        System.out.println("用户ID：" + userId);
        // 检查是否已经收藏课程
        boolean isCollected = videoCollectService.isCourseCollected(userId, courseId);
        boolean result;
        
        if (isCollected) {
            // 已收藏，执行取消收藏
            result = videoCollectService.uncollectCourse(userId, courseId);
            return result ? success("取消课程收藏成功") : error("取消课程收藏失败");
        } else {
            // 未收藏，执行收藏
            result = videoCollectService.collectCourse(userId, courseId);
            return result ? success("课程收藏成功") : error("课程收藏失败");
        }
    }

    /**
     * 取消收藏课程
     */
    @DeleteMapping("/course/{courseId}/collect")
    public AjaxResult uncollectCourse(@PathVariable("courseId") Long courseId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        boolean result = videoCollectService.uncollectCourse(userId, courseId);
        return result ? success("取消课程收藏成功") : error("取消课程收藏失败");
    }
}